﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Payton.WP7.Common.Interface;
using Newtonsoft.Json.Linq;
using Payton.WP7.Common.Model;
using System.Globalization;
using System.Diagnostics;

namespace Payton.WP7.Common.Renren
{
    public class RenrenJTokenToStatusMapping : MappingBase<JToken, SNSStatus>
    {
        public RenrenJTokenToStatusMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSStatus Map(JToken s)
        {
            try
            {
                var status = new RenrenSNSStatus()
                {
                    Account = _factory.Account,
                    Provider = SNSProviderType.RenRen,
                    Type = StatusType.Feed
                };
                var feedType = 0;
                if (s.SelectToken("update_time") != null)
                    status.CreatedTime = DateTime.ParseExact((string)s.SelectToken("update_time"), "yyyy-MM-dd HH:mm:ss", new CultureInfo("en-us"));
                if (s.SelectToken("feed_type") != null)
                    feedType = (int)s.SelectToken("feed_type");
                if (s.SelectToken("post_id") != null)
                    status.Id = (long)s.SelectToken("post_id");
                if (s.SelectToken("source_id ") != null)
                    status.SourceId = (long)s.SelectToken("source_id ");

                if (s.SelectToken("source_name") != null)
                    status.Source = (string)s.SelectToken("source_name");

            
                
                switch (feedType)
                {
                    case 10:
                    case 11:
                        if (s.SelectToken("message") != null)
                            status.Text = HttpUtility.HtmlDecode((string)s.SelectToken("message"));
                        break;
                    case 30:
                        if (s.SelectToken("attachment") != null && s.SelectToken("attachment").First != null)
                        {
                            var node = s.SelectToken("attachment").First;
                            if (node.SelectToken("media_type") != null)
                            {

                                var media_type = (string)node.SelectToken("media_type");
                                if (media_type == "photo" && node.SelectToken("src") != null)
                                {
                                    status.ThumbnailPic = (string)node.SelectToken("src");
                                    if (node.SelectToken("raw_src") != null)
                                        status.BmiddlePic = (string)node.SelectToken("raw_src");
                                    if (node.SelectToken("content") != null )
                                        status.Text = HttpUtility.HtmlDecode((string)node.SelectToken("content"));
                                }
                            }
                        }
                        if (s.SelectToken("prefix") != null)
                            status.Prefix = (string)s.SelectToken("prefix");
                        break;
                    case 51:
                        if (s.SelectToken("attachment") != null && s.SelectToken("attachment").First != null)
                        {
                            var node = s.SelectToken("attachment").First;
                            if (node.SelectToken("media_type") != null)
                            {
                                var media_type = (string)node.SelectToken("media_type");
                                if (media_type == "link" && node.SelectToken("src") != null)
                                {
                                    status.ThumbnailPic = (string)node.SelectToken("src");
                                    status.BmiddlePic = (string)node.SelectToken("src");

                                    if (s.SelectToken("description") != null && s.SelectToken("prefix") != null)
                                        status.Text = (string)s.SelectToken("description");
                                }
                            }
                        }
                        if (s.SelectToken("prefix") != null)
                            status.Prefix = (string)s.SelectToken("prefix");
                        break;
                }


                if (s.SelectToken("uid") != null || s.SelectToken("actor_id") != null)
                    status.User = _factory.GetMapping<JToken, SNSUser>().Map(s);
                status.CommentCount = 0;
                if (s.SelectToken("comments") != null && s.SelectToken("comments").SelectToken("count") != null)
                    status.CommentCount = (int)s.SelectToken("comments").SelectToken("count");
                //else if (s.SelectToken("comments") != null && s.SelectToken("comments").First != null && s.SelectToken("comments").First.SelectToken("comment_id") != null)
                //{
                //    foreach (var comment in s.SelectToken("comments").Values())
                //    {
                //        status.CommentCount++;
                //    }
                //}
                status.Source = "人人网";

                return status;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return null;
                //throw;
            }
        }
    }
}
